import { createWebHistory, createRouter } from 'vue-router'
import { useUserStore } from '@/stores/index'

// 创建路由实例
const router = createRouter({
  // 路由模式为history-> createWebHistory
  //  路由模式为hash -> createWebHashHistory
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      anme: 'login',
      component: () => import('@/views/login/loginPage.vue')
    },
    {
      path: '/',
      name: 'layout',
      component: () => import('@/views/layout/layoutContainer.vue'),
      redirect: '/article/manage',
      children: [
        {
          path: '/article/manage',
          name: 'articleManage',
          component: () => import('@/views/article/articleManage.vue')
        },
        {
          path: '/article/channel',
          name: 'articleChannel',
          component: () => import('@/views/article/articleChannel.vue')
        },
        {
          path: '/user/profile',
          name: 'userProfile',
          component: () => import('@/views/user/userProfile.vue')
        },
        {
          path: '/user/avatar',
          name: 'userAvatar',
          component: () => import('@/views/user/userAvatar.vue')
        },
        {
          path: '/user/password',
          name: 'userPassword',
          component: () => import('@/views/user/userPassword.vue')
        }
      ]
    }
  ]
})

// 路由拦截器
router.beforeEach((to) => {
  // 判断用户是否登录
  // 仓库实例
  const userStore = useUserStore()
  const token = userStore.token
  console.log(token)
  if (to.path !== '/login' && !token) {
    return '/login'
  }
})

export default router
